package logic

import (
	"context"
	"fmt"
	"rent/model/mysql"
	"rent/model/redis"
	"rent/rentrpc/internal/svc"
	"rent/rentrpc/rentrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type CollectLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCollectLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CollectLogic {
	return &CollectLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CollectLogic) Collect(in *rentrpc.CollectRequest) (*rentrpc.CollectResponse, error) {
	// todo: add your logic here and delete this line

	var product mysql.Product
	mysql.DB.Debug().Where("id = ?", in.Produce).Find(&product)

	if product.ID == 0 {
		return nil, fmt.Errorf("账号不存在")
	}

	redis.Client.ZIncrBy("collect", 1, product.Produce)

	return &rentrpc.CollectResponse{Id: in.UserId}, nil
}
